package com.asa.os;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class StreamTool {//流的开关时机可以优化
	private static final int SIZE = 4096;

	/**
	 * 会自动换行的
	 * 
	 * @param content 保存内容
	 * @param path    保存文件路径
	 */
	public static void getwriter(String content, String path) {
		FileOutputStream fop = null;
		File file;
//    	  String content = "This is the text content";
		try {
			file = new File(path);
			// if file doesnt exists, then create it
			if (!file.exists()) {
				file.createNewFile();
			}
			// get the content in bytes
			fop = new FileOutputStream(file, true);

			byte[] contentInBytes = content.getBytes();

			fop.write(contentInBytes);
			fop.write("\r\n".getBytes());
			fop.flush();
			fop.close();
			System.out.println("Done");
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (fop != null) {
					fop.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}
	public static void getwriterFile(String content, File file) {
		FileOutputStream fop = null;
//    	  String content = "This is the text content";
		try {
			// if file doesnt exists, then create it
			if (!file.exists()) {
				file.createNewFile();
			}
			fop = new FileOutputStream(file, true);

			// get the content in bytes

			byte[] contentInBytes = content.getBytes();

			fop.write(contentInBytes);
			fop.write("\r\n".getBytes());
			fop.flush();
			fop.close();
			System.out.println("Done");
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (fop != null) {
					fop.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}
	
	public static List<String> getreaderFile(File file,String code) {
		BufferedReader br = null;
		// 创建文件字节读取流对象时，必须明确与之关联的数据源。

		List<String> list = new ArrayList<String>();
		try {
			
//			fis = new FileInputStream(file);
			br=new BufferedReader( new InputStreamReader(new FileInputStream(file), code));
			// 创建一个字节数组，定义len记录长度
//			br.mark( ( int )file.length() + 1 );		//在首行做个标记
//			System.out.println("卡住了 吗？");
			while(br.read() != -1){
			      String str = br.readLine();     
//			      System.out.println(str);
			      list.add(str);
			 }
//			br.reset();
//			
//			while(br.read() != -1){
//				
//			      String str = br.readLine();     
//			      System.out.println("重新读取了"+str);
//			 }
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			// 关资源
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

		}
		return list;


	}
	public static void getreader(String path,String code) {

		// 为了确保文件一定在之前是存在的，将字符串路径封装成File对象
		File file = new File(path);
		if (!file.exists()) {
			throw new RuntimeException("要读取的文件不存在");
		}
//		FileInputStream fis = null;
		BufferedReader br = null;
		// 创建文件字节读取流对象时，必须明确与之关联的数据源。
		try {
//			fis = new FileInputStream(file);
			br=new BufferedReader( new InputStreamReader(new FileInputStream(file), code));
			// 创建一个字节数组，定义len记录长度
			
			while(br.read() != -1){
			      String str = br.readLine();     
//			      System.out.println(str);
			 }

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			// 关资源
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

		}

	}

	/**
	 * 不怎么好用
	 * 
	 * @param is InputStream
	 * @return
	 */
	public static String toString(InputStream is) {  
        
        try {  
            ByteArrayOutputStream boa=new ByteArrayOutputStream();  
            int len=0;  
            byte[] buffer=new byte[1024];  
              
            while((len=is.read(buffer))!=-1){  
                boa.write(buffer,0,len);  
            }  
            is.close();  
            boa.close();  
            byte[] result=boa.toByteArray();  
              
            String temp=new String(result);  
              
            //识别编码  
            if(temp.contains("utf-8")){  
                return new String(result,"utf-8");  
            }else if(temp.contains("gb2312")){  
                return new String(result,"gb2312");  
            }else if(temp.contains("gbk")){  
                return new String(result,"gbk");  
            }else{  
                return new String(result,"utf-8");  
            }
              
              
        } catch (Exception e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
            return null;  
        }  
          
    }  

}
